package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 9/11/2023 10:46 PM
 */
@Service
public class OverflowListService {
    @Autowired
    OverflowListDao overflowListDao;
    @Autowired
    OverflowListGoodsDao overflowListGoodsDao;
    public void save(HttpSession session, OverflowList overflowList, String overflowListGoodsStr) {
        //1.查询获取用户的userId
        User user = (User)session.getAttribute("currentUser");
        Integer userId = user.getUserId();

        //2.保存damageList对象
        overflowList.setUserId(userId);
        overflowListDao.save(overflowList);

        //3.保存damageListGoods对象(里面需要保存damageList的id)
        List<OverflowListGoods> overflowListGoods = JSON.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        if(!CollectionUtils.isEmpty(overflowListGoods)){
            //判断下不为空的时候再进行下一步的操作
            //遍历循环将对应的保存的商品保存到damageListGoods表里面
            overflowListGoods.forEach(overflowListGood -> {
                /* DamageListGoods overflowListGoods = new DamageListGoods();
                BeanUtils.copyProperties(good,overflowListGoods); */
                overflowListGood.setOverflowListId(overflowList.getOverflowListId());
                //将damageListGoods保存到数据库里面
                overflowListGoodsDao.save(overflowListGood);
            });
        }
    }

    public Map<String, Object> queryByTimeSection(String sTime, String eTime) {
        List<OverflowList> overflowLists = overflowListDao.queryByTimeSection(sTime,eTime);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",overflowLists);
        return map;
    }

    public Map<String, Object> queryOverflowListGoods(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoods = overflowListGoodsDao.queryOverflowListGoods(overflowListId);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",overflowListGoods);
        return map;
    }
}
